using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Npgsql;

namespace pgImport
{
    public partial class SelectDatabase : Form
    {
        private Boolean remember_pwd = false;
        private Boolean valid_connection_info = false;
        private String username = "postgres";
        private String password = "";
        private String dbase_name = "wintunes";
        private int port_no = 5432;
        private String hostname = "localhost";
 
        public SelectDatabase()
        {
            InitializeComponent();
        }

        #region Connection Properties

        public Boolean IsValidConnection
        {
            get { return valid_connection_info; }
        }

        public String DatabaseName
        {
            get { return dbase_name; }
        }

        public String UserName
        {
            get { return username;  }
        }

        public String Password
        {
            get { return password;  }
        }

        public Int32 PortNumber
        {
            get { return port_no; }
        }

        public String HostName
        {
            get { return hostname;  }
        }

        public String ConnectionString
        {
            get
            {
                StringBuilder tmp = new StringBuilder();

                // Build conn string
                tmp.Append("Server=" + hostname + ";");
                tmp.Append("Port=" + port_no + ";");
                tmp.Append("User Id=" + username + ";");
                tmp.Append("Password=" + password + ";");
                tmp.Append("Database=" + dbase_name + ";");

                return tmp.ToString();
            }
        }

        #endregion

        #region Event Handlers for controls

        /// <summary>
        /// Event handler for 'Remember Password' checkbox          
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void rememberpwdCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            remember_pwd = !remember_pwd;
        }

        /// <summary>
        /// Handles 'text changed' event for 'database name' text box
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dbasenameTextBox_TextChanged(object sender, EventArgs e)
        {
            dbase_name = dbasenameTextBox.Text;
            checkTestButtonVisible();
        }

        /// <summary>
        /// Handles 'text changed' event for 'user name' text box
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void usernameTextBox_TextChanged(object sender, EventArgs e)
        {
            username = usernameTextBox.Text;
            checkTestButtonVisible();
        }

        /// <summary>
        /// Handles 'text changed' event for 'password' text box
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void passwordTextBox_TextChanged(object sender, EventArgs e)
        {
            password = passwordTextBox.Text;
            checkTestButtonVisible();
        }

        /// <summary>
        /// Handles 'click' events for 'test conn' button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void testconnButton_Click(object sender, EventArgs e)
        {
            if (testDBConnection(dbase_name, username, password))
            {
                acceptButton.Enabled = true;
                MessageBox.Show("Successfully tested conn", "Connection OK", MessageBoxButtons.OK);
            }
            else
            {
                acceptButton.Enabled = false;
                MessageBox.Show("Invalid conn details", "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        /// <summary>
        /// Handles 'click' events for 'accept' button
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void acceptButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// Handles 'text changed' event for Hostname text box
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void hostnameTextBox_TextChanged(object sender, EventArgs e)
        {
            hostname = hostnameTextBox.Text;
        }

        /// <summary>
        /// Handled 'text changed' event for Port No. text box
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void portnumTextBox_TextChanged(object sender, EventArgs e)
        {
            try
            {
                port_no = Convert.ToInt32(portnumTextBox.Text);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        #endregion

        #region Private member routines

        /// <summary>
        /// Sets visiblity of 'Test Connection' button according to required fields
        /// </summary>
        private void checkTestButtonVisible()
        {
            if (dbase_name.Length > 0 && username.Length > 0 && password.Length > 0)
                testconnButton.Enabled = true;
            else
                testconnButton.Enabled = false;
        }

        private Boolean testDBConnection(String dbase_name, String user_name, String password)
        {
            StringBuilder   tmp = new StringBuilder();

            // Build conn string
            tmp.Append("Server=" + hostname + ";");
            tmp.Append("Port=" + port_no + ";");
            tmp.Append("User Id=" + username + ";");
            tmp.Append("Password=" + password + ";");
            tmp.Append("Database=" + dbase_name + ";");

            try
            {
                NpgsqlConnection conn = new NpgsqlConnection(tmp.ToString());
                conn.Open();
                conn.Close();

                acceptButton.Enabled = valid_connection_info = true;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                acceptButton.Enabled = valid_connection_info = false;
            }

            return valid_connection_info;
        }

        #endregion


    }
}